package cn.edu.ctbu.dp01.service;

import cn.edu.ctbu.dp01.constant.REnum;
import cn.edu.ctbu.dp01.dao.StudentRepository;
import cn.edu.ctbu.dp01.dao.string;
import cn.edu.ctbu.dp01.entity.Student;
import cn.edu.ctbu.dp01.exception.RException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.Comparator;
import java.util.List;

@Service
public class StudentService {

    @Autowired
    public StudentRepository studentRepository;


    public List<Student> getAll(){

        return studentRepository.findAll();
    }

    public Page<Student> getAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    public Page<Student> getAll( Example<Student> example,Pageable pageable){
        return studentRepository.findAll(example,pageable);
    }

    /**
     * 按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Student findById(Integer id){


        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 按name查询
     * @param name
     * @return
     */
    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }

    /**
     * 按name和sno查询
     * @param name
     * @param sno
     * @return
     */
    public List<Student> findByNameAndSno(String name,String sno){


        return studentRepository.findByNameAndSno(name,sno);
    }

    /**
     * 添加
     *
     * @param student The Student object containing the details to be saved.
     */
    public void insert(Student student){

        studentRepository.save(student);
    }

    /**
     * 更新
     * @param student
     */
    public void update(Student student){
         studentRepository.save(student);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id){
        studentRepository.deleteById(id);
    }

    /**
     * 查询学工号最大的老师
     * @return
     */
    public Student MaxSno() {
        List<Student> students = studentRepository.findAll();
        return students.stream()
                .max(Comparator.comparing(Student::getSno, Comparator.nullsLast(String::compareTo)))
                .orElse(null);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    public Student add(Student student) {
        return studentRepository.save(student);
    }

    public Student gx(Student student) {
        return studentRepository.save(student);
    }

    public void sc(Integer id) {
         studentRepository.deleteById(id);
    }

    public Student validateUsernameAndPassword(String sno,String password) throws Exception{
        List<Student> students=studentRepository.findBySno(sno);
        if (students.size()>0){
            //可能对password加密，但我们暂时不做处理
            Student student=students.get(0);
            if (student.getPassword().equals(password)){
                //成功
                return student;
            }else{
               throw new RException(REnum.LOGIN_ERR);
            }
        }else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }



}

